package com.sdy.healthcode.biz.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdy.healthcode.biz.constants.CacheConstants;
import com.sdy.healthcode.biz.mapper.RegionMapper;
import com.sdy.healthcode.biz.model.Region;
import com.sdy.healthcode.biz.service.RegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class RegionServiceImpl implements RegionService {
    @Autowired
    private RegionMapper regionMapper;

    @Override
    @Cacheable(cacheNames = CacheConstants.CacheNames.RegionService_listRegionByProvinceAndCity)
    public List<Region> listRegionByProvinceAndCity(String provinceName, String cityName) {
        return regionMapper.listRegionByProvinceAndCity(provinceName, cityName);
    }

    @Override
    @Cacheable(cacheNames = CacheConstants.CacheNames.RegionService_listAreaByProvinceAndCity)
    public List<Region> listAreaByProvinceAndCity(String provinceName, String cityName) {
        return regionMapper.listAreaByProvinceAndCity(provinceName, cityName);
    }

    @Override
    public IPage<Region> pageCommunityByArea(Page<Region> page, String areaCode) {
        int count = regionMapper.listCommunityByAreaCount(areaCode);
        List<Region> l = Collections.emptyList();
        if (count > 0) {
            l = regionMapper.listCommunityByArea(areaCode, (page.getCurrent() - 1) * page.getSize(), page.getSize());
        }
        return new Page<Region>(page.getCurrent(), page.getSize(), count).setRecords(l);
    }

    @Override
    public Region getProvince(String provinceName) {
        return regionMapper.getProvince(provinceName);
    }

    @Override
    public Region getCity(String cityName) {
        return regionMapper.getCity(cityName);
    }
}
